package com.ims.iaa;
//
// Copyright: Metaplus & Inquiry Management Systems Ltd.  2000
//
// Author: Michael Liao 
//

import java.util.*;
import java.io.*;

/**
 * This class represent http request.
 */
public class RequestParameters implements Serializable {

	private static final int HTTP_GET_METHOD = 0;
	private static final int HTTP_POST_METHOD = 1;

	//default method is GET
	private int method = HTTP_GET_METHOD;
	//defaut is allow value duplicate
	private boolean allowValueDuplicate = true;
	private Hashtable parameters = new Hashtable();
	//private String urlStr;
	

	/**
	 * Default constructor. This will create a RequestParameter which 
	 * allow value duplicate
	 */
	public RequestParameters( )  {
		super();
	}

	/**
	 * Use this constructor to create a RequestParameter which not allow value duplicate
	 * @param allowDuplicate true, will allow value duplicate, false will not allow
	 */
	public RequestParameters( boolean allowDuplicate )  {
		allowValueDuplicate = allowDuplicate;	
	}
	
	/**
	 * Set use POST method.
	 */
	public void setUsePostMethod()  {
		method = HTTP_POST_METHOD;
	}

	/**
	 * Set use GET method.
	 */
	public void setUseGetMethod()  {
		method = HTTP_GET_METHOD;
	}
	
	/**
	 * Test if using POST method.
	 */
	public boolean isPostMethod() {
		return (method==HTTP_POST_METHOD);
	}

	/**
	 * Test if using GET method.
	 */
	public boolean isGetMethod() {
		return (method==HTTP_GET_METHOD);
	}
	
	/**
	 * Return the parameters need to be sent out.
	 */
	public Hashtable getParameter() {
		return parameters;
	}
	
	/**
	 * Save a name-value pair into its parameters
	 */
	public void put( String name, String value ) {
	
		if( parameters.containsKey( name ) ) {
			// if it already contains the name, then we need to 
			// put value in a vector, because, same name, can has
			// multiple value in the real Web world.
			Object orgValue = parameters.get( name );
			
			if ( orgValue instanceof Vector ) {
				// if the value in hashtable is already a vector
				// just save the new value into that vector;
				Vector values = (Vector) orgValue;

				if ( allowValueDuplicate ) {
					values.addElement( value );
				} else  {
					if (!values.contains( value ) ) 
						values.addElement( value );
				}
			} else  {
				// if the value is not a vector, then we create one,
				// and save the original value and new value into that vector.
				Vector values = new Vector();
				values.addElement( orgValue );
				values.addElement( value );
				parameters.put( name, values );
			}
			
		} else  {
			parameters.put( name, value );
		}
	}

	/* The following two method is for testing
	
	public String toString() {
	
		StringWriter writer = new StringWriter();
		
		Enumeration keys = parameters.keys();
		while ( keys.hasMoreElements() )  {
			String name = (String) keys.nextElement();
			writer.write( name + "=");
			Object valueObj = parameters.get( name );
			
			if ( valueObj  instanceof Vector ) {
				Enumeration values = ((Vector)valueObj).elements();
				while( values.hasMoreElements())  {
					writer.write( values.nextElement() + ";");
				}
				
			} else {
				writer.write( (String) valueObj );
			}
			writer.write("\n");
		}
		return writer.toString();
	}
	
	public static void main( String[] args ) {
	
		RequestParameters req;
		req = new RequestParameters();
		req.put("firstName", "Smith");
		req.put("lastName","Black");
		req.put("parameters", "aaa");
		req.put("parameters", "bbb");
		req.put("parameters", "ccc");
		req.put("parameters", "aaa");
		req.put("parameters", "bbb");
		req.put("parameters", "ccc");
		IaaUtil.log( "A duplicate value parameter");
		IaaUtil.log( req );
		
		req = new RequestParameters( false );
		req.put("firstName", "Smith");
		req.put("lastName","Black");
		req.put("parameters", "aaa");
		req.put("parameters", "bbb");
		req.put("parameters", "ccc");
		req.put("parameters", "aaa");
		req.put("parameters", "bbb");
		req.put("parameters", "ccc");
		IaaUtil.log( "A non-duplicate value parameter");
		IaaUtil.log( req );
	}
	*/
}